{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "120ca918-7da1-45eb-bc46-20e0ebaf2cbb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T11:29:03.659957Z",
     "iopub.status.busy": "2024-06-10T11:29:03.659366Z",
     "iopub.status.idle": "2024-06-10T11:29:03.669361Z",
     "shell.execute_reply": "2024-06-10T11:29:03.667214Z",
     "shell.execute_reply.started": "2024-06-10T11:29:03.659913Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from IPython.display import Image"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2406813-af0c-4eda-8214-8c771be0cf4c",
   "metadata": {},
   "source": [
    "## case 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8d7f7726-4c72-41ed-aa2a-6a7ea75b27e3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T10:43:10.415295Z",
     "iopub.status.busy": "2024-06-10T10:43:10.414657Z",
     "iopub.status.idle": "2024-06-10T10:43:10.430646Z",
     "shell.execute_reply": "2024-06-10T10:43:10.428636Z",
     "shell.execute_reply.started": "2024-06-10T10:43:10.415252Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 1.,  2.,  3.,  4.],\n",
       "         [ 5.,  6.,  7.,  8.],\n",
       "         [ 9., 10., 11., 12.],\n",
       "         [13., 14., 15., 16.]]], requires_grad=True)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.arange(1, 17).reshape(4, 4).float()\n",
    "X.unsqueeze_(0)\n",
    "X.requires_grad_(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "80b326eb-477d-4503-956c-a696a35ded69",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T10:43:57.107448Z",
     "iopub.status.busy": "2024-06-10T10:43:57.106875Z",
     "iopub.status.idle": "2024-06-10T10:43:57.128889Z",
     "shell.execute_reply": "2024-06-10T10:43:57.126936Z",
     "shell.execute_reply.started": "2024-06-10T10:43:57.107404Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 6.,  8.],\n",
       "         [14., 16.]]], grad_fn=<MaxPool2DWithIndicesBackward0>)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pooled_X = torch.nn.functional.max_pool2d(X, kernel_size=(2, 2), stride=2)\n",
    "pooled_X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d094072f-ab94-4eab-a11a-ac4d52cf0b7a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T10:46:11.938897Z",
     "iopub.status.busy": "2024-06-10T10:46:11.938232Z",
     "iopub.status.idle": "2024-06-10T10:46:11.965436Z",
     "shell.execute_reply": "2024-06-10T10:46:11.963658Z",
     "shell.execute_reply.started": "2024-06-10T10:46:11.938812Z"
    }
   },
   "outputs": [],
   "source": [
    "y = torch.sum(pooled_X)\n",
    "y.backward()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e83f4cc-7f5e-40ad-b8cb-1b6c780fdedb",
   "metadata": {},
   "source": [
    "\n",
    "$y=\\max(a,b)$\n",
    "\n",
    "\\begin{equation}\n",
    "\\frac{\\partial y}{\\partial a} = \n",
    "\\begin{cases} \n",
    "1 & \\text{if } a \\geq b \\\\\n",
    "0 & \\text{else}\n",
    "\\end{cases}\n",
    "\\end{equation}\n",
    "\n",
    "\\begin{equation}\n",
    "\\frac{\\partial y}{\\partial b} = \n",
    "\\begin{cases} \n",
    "0 & \\text{if } a > b \\\\\n",
    "1 & \\text{else}\n",
    "\\end{cases}\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "07973209-45aa-4362-9a61-0ce7cde26445",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T10:46:19.662670Z",
     "iopub.status.busy": "2024-06-10T10:46:19.662057Z",
     "iopub.status.idle": "2024-06-10T10:46:19.676597Z",
     "shell.execute_reply": "2024-06-10T10:46:19.674518Z",
     "shell.execute_reply.started": "2024-06-10T10:46:19.662626Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 1., 0., 1.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 1., 0., 1.]]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "168129ca-a8f5-468b-8940-ae8ed2d6cd89",
   "metadata": {},
   "source": [
    "## case2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "719ae0cd-6d42-40dc-9915-ffe4b8664669",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T11:28:48.167954Z",
     "iopub.status.busy": "2024-06-10T11:28:48.167643Z",
     "iopub.status.idle": "2024-06-10T11:28:48.179885Z",
     "shell.execute_reply": "2024-06-10T11:28:48.177658Z",
     "shell.execute_reply.started": "2024-06-10T11:28:48.167934Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[1., 3., 2., 4.],\n",
       "          [5., 6., 1., 2.],\n",
       "          [7., 8., 3., 1.],\n",
       "          [2., 4., 6., 8.]]]], requires_grad=True)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([[[[1.0, 3.0, 2.0, 4.0],\n",
    "                    [5.0, 6.0, 1.0, 2.0],\n",
    "                    [7.0, 8.0, 3.0, 1.0],\n",
    "                    [2.0, 4.0, 6.0, 8.0]]]])\n",
    "x.requires_grad_(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "d048e7d7-fbdf-4680-a537-215ddc3ac61b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T11:28:49.284664Z",
     "iopub.status.busy": "2024-06-10T11:28:49.284080Z",
     "iopub.status.idle": "2024-06-10T11:28:49.293069Z",
     "shell.execute_reply": "2024-06-10T11:28:49.291296Z",
     "shell.execute_reply.started": "2024-06-10T11:28:49.284620Z"
    }
   },
   "outputs": [],
   "source": [
    "y, max_indices = F.max_pool2d(x, kernel_size=2, stride=2, return_indices=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "948aa726-875c-418a-a22a-5c28893e5bbd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T11:28:50.439663Z",
     "iopub.status.busy": "2024-06-10T11:28:50.439086Z",
     "iopub.status.idle": "2024-06-10T11:28:50.452927Z",
     "shell.execute_reply": "2024-06-10T11:28:50.450812Z",
     "shell.execute_reply.started": "2024-06-10T11:28:50.439620Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[[6., 4.],\n",
       "           [8., 8.]]]], grad_fn=<MaxPool2DWithIndicesBackward0>),\n",
       " tensor([[[[ 5,  3],\n",
       "           [ 9, 15]]]]))"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y, max_indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "bc80ad51-2c5a-43ad-b9cc-88ae49542676",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T11:28:51.717558Z",
     "iopub.status.busy": "2024-06-10T11:28:51.716986Z",
     "iopub.status.idle": "2024-06-10T11:28:51.730641Z",
     "shell.execute_reply": "2024-06-10T11:28:51.728598Z",
     "shell.execute_reply.started": "2024-06-10T11:28:51.717515Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[0., 0., 0., 1.],\n",
       "          [0., 1., 0., 0.],\n",
       "          [0., 1., 0., 0.],\n",
       "          [0., 0., 0., 1.]]]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.sum().backward()\n",
    "x.grad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "6ed63b3a-a566-4050-8447-595999a708d0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-06-10T11:29:22.974893Z",
     "iopub.status.busy": "2024-06-10T11:29:22.974237Z",
     "iopub.status.idle": "2024-06-10T11:29:22.987302Z",
     "shell.execute_reply": "2024-06-10T11:29:22.985179Z",
     "shell.execute_reply.started": "2024-06-10T11:29:22.974806Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"../../../imgs/maxpool_grad.png\" width=\"400\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image(url='../../../imgs/maxpool_grad.png', width=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "917ec865-0eea-4cb8-954b-ad4d8e03c78f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
